package 作业;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.Arrays;

public class editoruser extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JPasswordField pass1;
	private JLabel newagain;
	private JLabel lblNewLabel_1;
	private JPasswordField pass2;
	private JPasswordField pass3;

	public editoruser(userinformation info) {
		setTitle("密码修该");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 445, 576);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		
		JLabel start = new JLabel("原密码");
		start.setFont(new Font("宋体", Font.PLAIN, 20));
		
		pass1 = new JPasswordField();
		pass1.setColumns(10);
		
		newagain = new JLabel("新密码");
		newagain.setFont(new Font("宋体", Font.PLAIN, 20));
		
		lblNewLabel_1 = new JLabel("确认新密码");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 20));
		
		pass2 = new JPasswordField();
		pass2.setColumns(10);
		
		pass3 = new JPasswordField();
		pass3.setColumns(10);
		
		pass2.setEchoChar('#');
		pass3.setEchoChar('#');
		pass1.setEchoChar('#');
		
		JCheckBox showmima = new JCheckBox("显示密码");
		showmima.setSelected(false);
		showmima.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			if(!showmima.isSelected()) {
				pass2.setEchoChar('⬤');
				pass3.setEchoChar('⬤');
				pass1.setEchoChar('⬤');
			}
			else {
				pass2.setEchoChar('\0');
				pass3.setEchoChar('\0');
				pass1.setEchoChar('\0');
			}
			}
		});
		showmima.setFont(new Font("宋体", Font.PLAIN, 16));
		
		JLabel lblNewLabel_2 = new JLabel("修该密码");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 28));
		
		JButton queren = new JButton("确认修改");
		queren.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					if(new String(pass1.getPassword()).equals(Getdb.op.getuser(Getdb.username))) {
						if(Arrays.equals(pass2.getPassword(), pass3.getPassword())) {
							if(pass2.getPassword().length>=6) {
								try {
									Getdb.op.editor(Getdb.username,new String(pass2.getPassword()));
								} catch (SQLException ex) {
									throw new RuntimeException(ex);
								}
								JOptionPane.showMessageDialog(lblNewLabel_2, "修该成功","提示",JOptionPane.WARNING_MESSAGE);
								dispose();
							}
							else {
								JOptionPane.showMessageDialog(lblNewLabel_2, "密码太短","提示",JOptionPane.ERROR_MESSAGE);
							}
						}
						else {
							JOptionPane.showMessageDialog(lblNewLabel_2, "两次密码不同，修改失败","提示",JOptionPane.ERROR_MESSAGE);
						}
					}
					else {
						JOptionPane.showMessageDialog(lblNewLabel_2, "原密码不正确","提示",JOptionPane.ERROR_MESSAGE);
					}
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		queren.setFont(new Font("宋体", Font.PLAIN, 20));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(147)
					.addComponent(queren, GroupLayout.PREFERRED_SIZE, 169, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(170, Short.MAX_VALUE))
				.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(46)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(start, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
								.addComponent(newagain, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(24)
							.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 221, GroupLayout.PREFERRED_SIZE)
						.addComponent(showmima, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
						.addComponent(pass3, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE)
						.addComponent(pass2, GroupLayout.PREFERRED_SIZE, 247, GroupLayout.PREFERRED_SIZE)
						.addComponent(pass1, GroupLayout.PREFERRED_SIZE, 247, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(69, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(53)
					.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
					.addGap(43)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(start, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
						.addComponent(pass1, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
					.addGap(37)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(newagain, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
							.addGap(34)
							.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(pass2, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(pass3, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)))
					.addGap(33)
					.addComponent(showmima)
					.addGap(18)
					.addComponent(queren, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(121, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
